<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a
        href="javascript:;" :class="{active:sorter===null}"
        >推荐</a
      >
      <a
        href="javascript:;" @click="handle('weight_desc')" :class="{active:sorter==='weight_desc'}"
        >最新</a
      >
      <div class="logo"><img src="@/assets/logo.png" alt=""></div>
    </nav>

    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
>
<articleItem v-for="item in articleList" :key="item.id" :item="item"  ></articleItem>
</van-list>
  </div>
</template>

<script>
import { articleListAPI } from '@/api/article'

export default {
  name: 'article-page',
  data () {
    return {
      articleList: [],
      list: [],
      loading: false,
      finished: false,
      current: 39,
      pageSize: 10,
      sorter: 'weight_desc'
    }
  },
  created () {

  },
  methods: {
    handle2 () {
      console.log('   出发了')
    },

    async onLoad () {
      const res = await articleListAPI({
        current: this.current,
        pageSize: this.pageSize,
        sorter: this.sorter
      })
      this.current++
      console.log(res, '全部数据')
      console.log(this.loading, '底部触发', 35)
      this.articleList.push(...res.data.rows)
      // 一共是42条数据，但是最后一条我们是++，也就是加到了43  所以直接当前大于总计42结束请求。而不是大于等于
      if (this.current > res.data.pageTotal) {
        this.finished = true
      }
      console.log(this.current, res.data.pageTotal)
      console.log(this.articleList, '底部触发关闭', 54)
      // 关闭加载
      this.loading = false
    },
    handle (sorter) {
      // 重置数据
      this.sorter = sorter
      this.current = 1
      this.finished = false
      this.articleList = []
      // 重新获取数据
      this.onLoad()
    }
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}

</style>
